<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="PackageList.aspx.cs" Inherits="PackageList" Title="ABC SuperSign - Package List" %>
<%@ Register Src="UserAccess.ascx" TagName="UserAccess" TagPrefix="uc1" %>
<%@ Register Src="GlobalAjax.ascx" TagName="GlobalAjax" TagPrefix="uc2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_Main" Runat="Server">
<form id="Form_PackageList" runat="server"> 
<asp:HiddenField ID="Hidden_BtnChoice" runat="server" />    
<asp:ScriptManager ID="ScriptManager_PackageList" runat="server">
</asp:ScriptManager>  
<uc2:GlobalAjax ID="ctl_GlobalAjax" runat="server" />
  
<!-- page title start -->
<h1>
    <span>Package List</span>
    <asp:Button ID="Btn_New" runat="server" Text="Create New Package" CssClass="aspButton" PostBackUrl="~/Editor.aspx" OnClick="Btn_New_Click" />
</h1>
<!-- page title end -->
<!-- main content start -->
<div class="mainHolder">
<!-- left block start -->
<div class="leftBlock">
    <h2>
	    <span>Find Packages to Edit</span>
    </h2>
    <div class="innerBlock" style="overflow:visible">
	    <h3>
		    <span>Search Packages</span>
		    <a href="#" class="switch">Open/Collapse</a>
	    </h3>
		<div class="innerContent">
            <div id="packageList_SearchFields"></div>
            <div id="packageSearchButtons" class="row" style="display:none;">
                <asp:Button ID="Btn_Search" runat="server" Text="Search" OnClientClick="prepareSearch();" OnClick="Btn_Search_Click" />
                <div class="spacerS"></div>
                <input type="button" name="reset" value="Reset" onclick="resetSearchFields('packageList_SearchFields');"/>
            </div>
		</div>
	</div>
    <div class="innerBlock">
	    <h3>
		    <span>Search Results</span>
		    <a href="#" class="switch">Open/Collapse</a>
	    </h3>
        <asp:UpdatePanel ID="UpdatePanel_Results" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Btn_Search" EventName="Click"></asp:AsyncPostBackTrigger>
                <asp:AsyncPostBackTrigger ControlID="DDList_Sort" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
                <asp:AsyncPostBackTrigger ControlID="ListBox_Results" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
            </Triggers>
            <ContentTemplate>
                <div class="innerContent">
                    <div class="row">
                        <asp:Label ID="Label_PackageSearchAlerts" runat="server" Text=""></asp:Label>
                    </div>
                    <div class="row">
                        <div class="col">
                            <asp:DropDownList ID="DDList_Sort" runat="server" AutoPostBack="true" CssClass="small"
                                OnSelectedIndexChanged="DDList_Sort_SelectedIndexChanged">
                                <asp:ListItem Value="0" Text="Sort By" />
                                <asp:ListItem Value="1" Text="Name" />
                                <asp:ListItem Value="2" Text="Newest" />
                            </asp:DropDownList>
                        </div>
                        <div style="float: right" class="col">
                            <asp:Button ID="Btn_Add" OnClick="Btn_Add_Click" runat="server" CssClass="aspButton"
                                Text="Add >>" OnClientClick="addClick();" Enabled="false"></asp:Button>
                        </div>
                    </div>
                    <div class="row">
                        <asp:ListBox ID="ListBox_Results" runat="server" CssClass="heightL width100" AutoPostBack="true" OnSelectedIndexChanged="ListBox_Results_SelectedIndexChanged"></asp:ListBox>
                    </div>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</div>
<!-- left block end -->
<!-- right block start -->
<div class="rightBlock">
    <H2>
        <SPAN>Selected Packages</SPAN>
    </H2>
    <div class="innerContent">
        <div class="row">
            <asp:Label ID="Label_PackageAlerts" runat="server" Text=""></asp:Label>
        </div>
        <div class="row">
    <asp:UpdatePanel ID="UpdatePanel_Details" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Btn_Add" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="Btn_Remove" EventName="Click" />
    </Triggers>
    <ContentTemplate>
        <div class="col widthXM">
            <div class="row">
                <LABEL class="big" for="sr"><SPAN>Packages (3 Maximum)</SPAN> </LABEL>
                <asp:ListBox id="ListBox_Selected" runat="server" CssClass="heightL widthXM" DataValueField="PackageID" DataTextField="Name" DataSourceID=""></asp:ListBox> 
            </div>
            <div class="row"><asp:Button id="Btn_EditAll" onclick="Btn_EditAll_Click" runat="server" Text="Edit All Packages in List" CssClass="widthXM" PostBackUrl="~/MultiPackage.aspx" enabled="false"></asp:Button> 
            </div>
        </div>
        <div class="spacerXL"></div>
        <div class="col">
            <div class="row">
                <LABEL class="big" for="sr">Package Info</LABEL> 
                <asp:Panel id="Panel_PackageInfo" runat="server" CssClass="aspPanel heightM widthXM">
                    <asp:Label ID="NameLabel" runat="server" Text=""></asp:Label>
                </asp:Panel> 
            </div>
        </div>
    </ContentTemplate>
    </asp:UpdatePanel>
        <div class="spacerXL"></div>
        <div class="col">
            <div class="row">
                <LABEL>&nbsp;</LABEL> 
            </div>
            <div class="row">
                <asp:Button id="Btn_Edit" runat="server" Text="Edit Selected Package" CssClass="widthM" PostBackUrl="~/Editor.aspx" OnClick="Btn_Edit_Click"></asp:Button>
            </div>
            <div id="btnDuplicateRow" class="row"  style="display:none;" >
                <input type="button" id="btnDuplicate" disabled="disabled" class="widthM" value="Duplicate Package As..." onclick="prepareDuplicateAs();" />
            </div>
            <div id="btnDisableRow" class="row" style="display:none;" >
                <input type="button" id="btnDisable" disabled="disabled" class="widthM" value="Disable Package" onclick="prepareDisable();" />
            </div>
            <div id="btnEnableRow" class="row" style="display:none;" >
                <input type="button" id="btnEnable" disabled="disabled" class="widthM" value="Enable Package" onclick="prepareEnable();" />
            </div>
            <div class="row">
                <asp:UpdatePanel ID="UpdatePanel_RemoveBtn" runat="server" UpdateMode="Conditional">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Btn_Remove" EventName="Click" />
                </Triggers>
                <ContentTemplate>
                    <asp:Button id="Btn_Remove" OnClientClick="removeClick();" OnClick="Btn_Remove_Click" runat="server" Text="Remove Package from List" Cssclass="widthM"></asp:Button>
                </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </div>
    </div>
    </div>
</div>
<!-- right block end -->
</div>
<!-- main content end -->

<script type="text/javascript">
    document.onclick =  function () {
            $('<%= Label_PackageAlerts.ClientID %>').style.display = "none";
            $('<%= Label_PackageSearchAlerts.ClientID %>').style.display = "none";
        }

    // wgo !!! testing out a doubleclick client side add function
    function addPackage() {
        // double click to add package from results list to selected list
        var selectedBox = $('<%= ListBox_Selected.ClientID %>'); 
        
        if (selectedBox.options.length > 2) return;
    
        var resultsBox = $('<%= ListBox_Results.ClientID %>'); 
                
        var selectedResultText = resultsBox.options[resultsBox.selectedIndex].text   
        var selectedResultValue = resultsBox.options[resultsBox.selectedIndex].value
        
        var optionEl = document.createElement('option');
        optionEl.text = selectedResultText;
        optionEl.value = selectedResultValue;
        
        selectedBox.options.add(optionEl);   
    }
    
    function loadPackageSearchFields () {
    	
        // make calls to load search params    
        var sProc = "<STOREDPROCEDURE Name=\"sproc_WEB_GetSearchFields\" Level=\"1\" />";
        callAjaxPost(scheduleSFRequest, sProc, processScheduleSFRequestChange);
        
        sProc = "<STOREDPROCEDURE Name=\"sproc_WEB_GetSearchFields\" Level=\"2\" />";
        callAjaxPost(packageSFRequest, sProc, processPackageSFRequestChange);
        
        sProc = "<STOREDPROCEDURE Name=\"sproc_WEB_GetSearchFields\" Level=\"3\" />";
        callAjaxPost(eventSFRequest, sProc, processEventSFRequestChange);
        
        sProc = "<STOREDPROCEDURE Name=\"sproc_WEB_GetSearchFields\" Level=\"4\" />";
        callAjaxPost(comboSFRequest, sProc, processComboSFRequestChange);
        
    }
    activeEditorPageId = "packageList";
    loadPackageSearchFields(); 
    
    // event handler for index change on selected listbox
    function selectedIndexChange() {
        var selectList = $('<%= ListBox_Selected.ClientID %>');
        // request info for selected item
        requestGetInfo(selectList.options[selectList.selectedIndex].value, 1, $('<%= Panel_PackageInfo.ClientID %>'));
        if (selectList.selectedIndex != -1) {
            $('<%= Btn_Edit.ClientID %>').removeAttribute('disabled');
            $('<%= Btn_Remove.ClientID %>').removeAttribute('disabled');
            $('btnDuplicate').removeAttribute('disabled');
            if (selectList.options[selectList.selectedIndex].hasClassName('disabledOption')) {
                $('btnDisable').setAttribute('disabled', 'disabled');
                $('btnEnable').removeAttribute('disabled');
            } else {
                $('btnDisable').removeAttribute('disabled');
                $('btnEnable').setAttribute('disabled', 'disabled');
            }
        } 
    }
    
    // event handler for add or remove click
    function addClick() {
        $('<%= Btn_Edit.ClientID %>').setAttribute('disabled', 'disabled');
        $('<%= Btn_Remove.ClientID %>').setAttribute('disabled', 'disabled');
        $('btnDuplicate').setAttribute('disabled', 'disabled');
        $('btnDisable').setAttribute('disabled', 'disabled');
        $('btnEnable').setAttribute('disabled', 'disabled');
    }
    
    // event handler for add or remove click
    function removeClick() {
        $('<%= Btn_Edit.ClientID %>').setAttribute('disabled', 'disabled');
        //$('<%= Btn_Remove.ClientID %>').setAttribute('disabled', 'disabled');
        $('btnDuplicate').setAttribute('disabled', 'disabled');
        $('btnDisable').setAttribute('disabled', 'disabled');
        $('btnEnable').setAttribute('disabled', 'disabled');
    }
    
    // function to prepare duplicate as popup
	function prepareDuplicateAs() {
	    var selectList = $('<%= ListBox_Selected.ClientID %>');
        var name = selectList.options[selectList.selectedIndex].text;
        var dbId = selectList.options[selectList.selectedIndex].value;
        var msgNode = $('<%= Label_PackageAlerts.ClientID %>');
        $('dupAlert').style.display = "none";
	    showDuplicateAs('package', name, dbId, msgNode);
	}   
	
	// function to prepare call to disable object
	function prepareDisable() {
	    var selectList = $('<%= ListBox_Selected.ClientID %>');
        var dbId = selectList.options[selectList.selectedIndex].value;
        var msgNode = $('<%= Label_PackageAlerts.ClientID %>');
	    requestEnableDisable(dbId, 1, false, msgNode)
	}
	
	// function to prepare call to enable object
	function prepareEnable() {
	    var selectList = $('<%= ListBox_Selected.ClientID %>');
        var dbId = selectList.options[selectList.selectedIndex].value;
        var msgNode = $('<%= Label_PackageAlerts.ClientID %>');
	    requestEnableDisable(dbId, 1, true, msgNode)
	}	
	
	// function to prepare search
	var packageSearchRequest = null;
	function prepareSearch() {
	    packageSearchRequest = new XMLHttpRequest();
        $('pageContainer').addClassName('fadedBG');
        $('searchingMsg').style.display = "block";
        // add values to search xml 
        addSearchValuesToXML();  

        var url = "ListSearchPost.aspx";
        packageSearchRequest.open('POST', url, false);
        packageSearchRequest.setRequestHeader("Content-Type", "text/xml");
        packageSearchRequest.send(sfXML);
        if (packageSearchRequest.readyState == 4) {
		    if (packageSearchRequest.status == 200) {
		        $('pageContainer').removeClassName('fadedBG');
	            $('searchingMsg').style.display = "none";
		        return;
		    }
		}
	}

</script>
<uc1:UserAccess ID="UserAccess1" runat="server" />
</form>
</asp:Content>
